#include "head.h"

// 登录
int to_login(messenger_t* msg, int sock_fd, MYSQL* mysql)
{
    // 切割参数
    char* passwd = msg->args;
    char* usr = msg->args;

    while(*usr != '&') ++usr;
    *usr++ = '\0';  

    // 验证用户名 和 密码
    int ret = verify_log(usr, passwd, mysql);
    if(ret == 1){
        // 用户不存在
        send(sock_fd, "username", 8, MSG_NOSIGNAL);
    }else if(ret == -1){
        // 密码错误
        send(sock_fd, "faileded", 8, MSG_NOSIGNAL);
    }else if(ret == 0){
        // 成功
        send(sock_fd, "succeedd", 8, MSG_NOSIGNAL);
        // 对 uid 加密
        int uid = usr_id(mysql, usr);
        // // 发一个 token
        char jwt[1024] = {0};
        size_t jwt_lenth = 0;
        l8w8jwt(jwt, &jwt_lenth, uid);
        send(sock_fd, &jwt_lenth, sizeof(size_t), MSG_NOSIGNAL);
        send(sock_fd, &jwt, jwt_lenth, MSG_NOSIGNAL);
        
        HONG("info", "%s登陆成功", usr);
    }
    
    return 0;
}

// 注册
int to_register_(messenger_t* msg, int sock_fd, MYSQL* mysql){
    // 切割参数
    char* passwd = msg->args;
    char* usr = msg->args;

    while(*usr != '&') ++usr;
    *usr++ = '\0';  

    // 验证用户是否存在
    int ret = verify_reg(usr, passwd, mysql);

    // 失败
    if(ret == -1){
        send(sock_fd, "faileded", 8, MSG_NOSIGNAL);
        return 0;
    }   

    // 成功
    char salt_value[20] = "$6$";
    char* hash = NULL;
    srand(time(NULL));

    // 得到随机盐值
    for(int i = 0; i < 8; ++i){
        char c;
        do{
            c = rand() % 128;
        }while(isalnum(c) == false);
        salt_value[3 + i] = c;
    }  
    salt_value[11] = '$';  

    // 哈希
    hash = crypt(passwd, salt_value);
        
    char sql[1024] = {0};
    sprintf(sql, "insert into usr values (NULL,'%s','%s','%s')", usr, hash, salt_value);
    if(mysql_query(mysql, sql) != 0)
        HONG("error", "mysql_query%s", "");
    
    // 神经病hash
    hash=NULL;

    bzero(sql, sizeof(sql));
    sprintf(sql, "select * from usr where usr_name = '%s'", usr);
    if(mysql_query(mysql, sql) != 0)
        HONG("error", "mysql_query%s", "");

    MYSQL_RES* res = mysql_store_result(mysql);
    MYSQL_ROW line = mysql_fetch_row(res);
    
    bzero(sql, sizeof(sql));
    sprintf(sql, "insert into file values (NULL, '/', '%d', '/', 0, NULL, 1, -1, 0, NULL)", atoi(line[0]));
    mysql_free_result(res);
   
    if(mysql_query(mysql, sql) != 0)
        HONG("error", "mysql_query%s", "");
        
    send(sock_fd, "succeedd", 8, MSG_NOSIGNAL);

    int uid = usr_id(mysql, usr);
    // // 发一个 token
    char jwt[1024] = {0};
    size_t jwt_lenth = 0;
    l8w8jwt(jwt, &jwt_lenth, uid);
    send(sock_fd, &jwt_lenth, sizeof(size_t), MSG_NOSIGNAL);
    send(sock_fd, &jwt, jwt_lenth, MSG_NOSIGNAL);

    HONG("info", "%s注册成功", usr);
    return 0;
}

// 登陆验证
int verify_log(const char* usr, const char* passwd, MYSQL* mysql){
    // 执行 sql 语句
    char sql[100] = {0};
    sprintf(sql, "select * from usr where usr_name = '%s'", usr);
    if(mysql_query(mysql, sql) != 0)
        HONG("error", "mysql_query%s", "");
    
    // sql 执行成功， 取结果
    MYSQL_RES* res = mysql_store_result(mysql);
    if(mysql_num_rows(res) == 0 || res == NULL){   
        // 用户不存在
        mysql_free_result(res);
        return 1;
    }
    MYSQL_ROW line = mysql_fetch_row(res);

    // passwd 明文盐值哈希之后的密码
    // 得到盐值
    char salt_value[20] = {0};
    strcpy(salt_value, line[3]);

    // 加盐哈希
    char* hash = crypt(passwd, salt_value);

    // 密码正确
    if(strcmp(line[2], hash) == 0){
        mysql_free_result(res);
        return 0;
    }
    
    // 密码错误
    mysql_free_result(res);
    return -1;
}

// 验证注册
int verify_reg(const char* usr, const char* passwd, MYSQL* mysql){
    // 执行 sql 语句
    char sql[100] = {0};
    sprintf(sql, "select * from usr where usr_name = '%s'", usr);
    if(mysql_query(mysql, sql) != 0)
        HONG("error", "mysql_query%s", "");

    MYSQL_RES* res = mysql_store_result(mysql);
    if(res != NULL && mysql_num_rows(res) == 1)
    {
        // 用户存在
        mysql_free_result(res);
        return -1;
    }
    
    // 用户不存在
    mysql_free_result(res);
    return 0;
}